package com.frogs.ucenter.service.impl;

import java.util.Arrays;

import org.apache.commons.lang.ArrayUtils;
import org.jsoup.Connection.Method;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.frogs.system.util.ApiUrlParamEnum;
import com.frogs.system.web.api.entity.ApiUrl;
import com.frogs.ucenter.service.AbsService;
import com.frogs.ucenter.service.UserRechargeableCardService;

@Service("userRechargeableCardService")
public class UserRechargeableCardServiceImpl extends AbsService implements UserRechargeableCardService {
	
	private static Logger log = LoggerFactory.getLogger(UserRechargeableCardServiceImpl.class);
	
	/**
	  充值卡充值
	 */
	@Override
	public JSONObject consume(Integer userId, String rechargeCard, String passwd, String rechargeFrom) throws Exception {
		ApiUrl apiUrl = this.buildUcenterApiBaseUrl("USER_RECHARGEABLE_CARD_CONSUME");
		String[] otherParams = {ApiUrlParamEnum.userId.getParamName(), userId.toString(),
				ApiUrlParamEnum.rechargeCard.getParamName(), rechargeCard, 
				ApiUrlParamEnum.rechargeFrom.getParamName(), rechargeFrom,
				ApiUrlParamEnum.password.getParamName(),passwd,
				 };
		String[] allParams = (String[])ArrayUtils.addAll(apiUrl.getKeyvals(), otherParams);
		
		JSONObject result = null;
		try{
			return (JSONObject)this.requestApi(apiUrl.getUrl(), Method.POST, allParams);
		} catch (IllegalStateException e) {
			log.error("func[addUserRechargeRecord] url[{}] params[{}] exception[{} - {}] desc[fail]",
					new Object[] { apiUrl.getUrl(),Arrays.deepToString(allParams), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		} catch(Exception e){
			throw new Exception("充值卡充值接口请求失败！");
		}
		return null;
	}
	
}
